package com.mxpio.erp.oa.controller;

import com.mxpio.erp.common.oa.entity.OaTravelReimbursement;
import com.mxpio.erp.common.oa.entity.OaTravelReimbursementLine;
import com.mxpio.erp.common.oa.service.OaTravelReimbursementLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "OaTravelReimbursementLineController", description = "差旅报销单-明细")
@RestController
@RequestMapping("/erp/oa/travel/reim/line/")
public class OaTravelReimbursementLineController {

    @Autowired
    private OaTravelReimbursementLineService oaTravelReimbursementLineService;

    @GetMapping("list")
    @Operation(summary = "差旅报销单列表-明细", description = "差旅报销单列表-明细（无分页）", method = "GET")
    public Result<List<OaTravelReimbursementLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<OaTravelReimbursementLine> list = oaTravelReimbursementLineService.list(OaTravelReimbursementLine.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "差旅报销单列表-明细", description = "差旅报销单列表-明细（分页）", method = "GET")
    public Result<Page<OaTravelReimbursementLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<OaTravelReimbursementLine> pageResult = oaTravelReimbursementLineService.listPage(OaTravelReimbursementLine.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取差旅报销单列表-明细", description = "根据bizNo获取差旅报销单列表-明细", method = "GET")
    public Result<List<OaTravelReimbursementLine>> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        List<OaTravelReimbursementLine> reimbursementLine = oaTravelReimbursementLineService.list(OaTravelReimbursementLine.class, Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo));
        return Result.OK(reimbursementLine);
    }

    @RequestMapping("add")
    @Operation(summary = "保存差旅报销单列表-明细", description = "保存差旅报销单列表-明细")
    public Result<OaTravelReimbursementLine> add(@RequestBody OaTravelReimbursementLine oaTravelReimbursementLine) {
        oaTravelReimbursementLineService.save(oaTravelReimbursementLine);
        return Result.OK(oaTravelReimbursementLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑", description = "编辑", method = "PUT")
    public Result<OaTravelReimbursementLine> edit(@RequestBody OaTravelReimbursementLine oaTravelReimbursementLine) {
        oaTravelReimbursementLineService.update(oaTravelReimbursementLine);
        return Result.OK(oaTravelReimbursementLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<OaTravelReimbursement> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            oaTravelReimbursementLineService.delete(OaTravelReimbursementLine.class, key);
        }
        return Result.OK();
    }

}
